package com.example.webpractice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.webpractice.annotation.IgnoreAuth;
import com.example.webpractice.dto.LoginDTO;
import com.example.webpractice.dto.UpdatePwdDTO;
import com.example.webpractice.dto.XueshengDTO;
import com.example.webpractice.po.XueshengPO;
import com.example.webpractice.service.TokenService;
import com.example.webpractice.service.XueshengService;
import com.example.webpractice.utils.ResponseEnum;
import com.example.webpractice.utils.ServerResponseEntity;
import com.example.webpractice.vo.XueshengVO;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

import static com.example.webpractice.utils.ResponseEnum.*;

/**
 * 学生
 * 后端接口
 */
@RestController
@RequestMapping("/xuesheng")
public class XueshengController {
    @Autowired
    private XueshengService xueshengService;

    @Autowired
    private TokenService tokenService;

    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public ServerResponseEntity page(@RequestParam(name = "page") Integer page,
                                     @RequestParam(name = "limit") Integer limit,
                                     @RequestParam(name = "xuehao", required = false) String xuehao,
                                     @RequestParam(name = "xueshengxingming", required = false) String xueshengxingming) {
        QueryWrapper<XueshengPO> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(xuehao)) {
            queryWrapper.like("xuehao", xuehao);
        }
        if (StringUtils.isNotBlank(xueshengxingming)) {
            queryWrapper.like("xueshengxingming", xueshengxingming);
        }
        return ServerResponseEntity.success(xueshengService.queryPage(page, limit, queryWrapper));
    }


    /**
     * 详情
     */
    @RequestMapping("/info/{id}")
    public ServerResponseEntity info(@PathVariable("id") Long id) {
        XueshengVO xueshengVo = xueshengService.selectById(id);
        return ServerResponseEntity.success(xueshengVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ServerResponseEntity save(@RequestBody XueshengDTO xueshengDTO) {
        XueshengVO xueshengVO = xueshengService.selectByXuehao(xueshengDTO.getXuehao());
        if (xueshengVO != null) {
            return ServerResponseEntity.fail(accountFound);
        }
        xueshengService.create(xueshengDTO);
        return ServerResponseEntity.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    public ServerResponseEntity update(@RequestBody XueshengDTO xueshengDTO) {
        if (xueshengService.updateXuesheng(xueshengDTO) == 1) {
            return ServerResponseEntity.success();
        } else {
            return ServerResponseEntity.fail(updateFail);
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ServerResponseEntity delete(@RequestBody Long[] ids) {
        if (xueshengService.deleteBatchIds(Arrays.asList(ids)) != ids.length) {
            return ServerResponseEntity.fail(deleteFail);
        }
        return ServerResponseEntity.success();
    }

    /**
     * 获取用户的session用户信息
     */
    @RequestMapping("/session")
    public ServerResponseEntity getCurrUser(HttpServletRequest request) {
        Long id = (Long) request.getAttribute("userId");
        XueshengVO xueshengVO = xueshengService.selectById(id);
        return ServerResponseEntity.success(xueshengVO);
    }


    /**
     * 密码修改
     */
    @IgnoreAuth
    @RequestMapping(value = "/updatePasswd")
    public ServerResponseEntity resetPass(@RequestBody UpdatePwdDTO updatePwdDTO) {
        XueshengPO xueshengPO = xueshengService.selectByUsername(updatePwdDTO.getUsername());
        if (xueshengPO == null) {
            return ServerResponseEntity.fail(ResponseEnum.accountNotFound);
        }
        if (!xueshengPO.getMima().equals(updatePwdDTO.getOldpasswd())) {
            return ServerResponseEntity.fail(ResponseEnum.loginFail);
        }
        xueshengPO.setMima(updatePwdDTO.getNewpasswd());
        if (xueshengService.updateById(xueshengPO) != 1) {
            return ServerResponseEntity.fail(-1, "更新密码失败");
        }
        return ServerResponseEntity.success();
    }

    /**
     * 登录
     */
    @IgnoreAuth
    @PostMapping(value = "/login")
    public ServerResponseEntity login(@RequestBody LoginDTO loginDTO) {
        XueshengPO xueshengPO = xueshengService.selectByUsername(loginDTO.getUsername());
        if (xueshengPO == null || !xueshengPO.getMima().equals(loginDTO.getPassword())) {
            return ServerResponseEntity.fail(ResponseEnum.loginFail);
        }
        String token = tokenService.generateToken(xueshengPO.getId(), loginDTO.getUsername(), "xuesheng", xueshengPO.getRole());
        return ServerResponseEntity.success(token);
    }
}
